package whf.framework.web.tag;

import java.util.Map;

import javax.servlet.jsp.JspException;

import whf.framework.web.tag.ext.TagStructureException;

/**
 * @author wanghaifeng
 *
 */
public class TableFooterTag extends HtmlTag {

	private static final long serialVersionUID = 899149338534L;

	public int doEndTag() throws JspException {
		TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
		if (tableTag == null) {
			throw new TagStructureException(getClass(), "footer", "table");
		}
		if (tableTag.isLastIteration()) {
			if (getBodyContent() != null) {
				tableTag.setFooter(getBodyContent().getString());
			}
		}
		return EVAL_PAGE;
	}

	public int doStartTag() throws JspException {
		TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
		if (tableTag == null) {
			throw new TagStructureException(getClass(), "footer", "table");
		}
		if (tableTag.isLastIteration()) {
			if (tableTag.getVarTotals() != null) {
				Map totals = tableTag.getTotals();
				this.pageContext.setAttribute(tableTag.getVarTotals(), totals);
			}
			return 2;
		}
		super.release();
		return SKIP_BODY;
	}

	public void release() {
		super.release();
	}
}